package cn.lyjuan.first.netty.demo._3_netty3.server;

import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.nio.NioSocketChannel;

import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * 服务端核心处理程序
 */
public class ServerHandler extends SimpleChannelHandler
{
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception
    {
        String msg = (String) e.getMessage();

        System.out.println("server messageReceived: " + msg);

        // write
        NioSocketChannel chl = (NioSocketChannel) ctx.getChannel();
        chl.write("hi");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception
    {
        System.out.println("server exceptionCaught");
        super.exceptionCaught(ctx, e);
    }

    @Override
    public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
    {
        System.out.println("server channelConnected");
        super.channelConnected(ctx, e);
    }

    @Override
    public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
    {
        System.out.println("server channelDisconnected");
        super.channelDisconnected(ctx, e);
    }
}
